import mysql.connector


def get_con():
    try:
        con = mysql.connector.connect(
            host='127.0.0.1',
            user='root',
            password='123456',
            port='3306',
            charset='utf8',
            database='examtest'
        )
    except mysql.connector.Error:
        print('数据库连接失败')
    else:
        return con


def get_sql_content(table_name, *args):
    print(args)
    print(len(args))
    """
    :param table_name: 表名
    :param args: 字段元组
    :return: 查询的数据集
    """
    con = get_con()
    sql = 'select '
    if len(args) != 0:
        for i in range(0, len(args)):
            if i == len(args) - 1:
                sql += args[i]
            else:
                sql += args[i]
                sql += ','
    sql += " from " + table_name

    print(sql)
    cursor = con.cursor()
    cursor.execute(sql)
    values = cursor.fetchall()
    cursor.close()
    con.close()
    return values


if __name__ == '__main__':
    print(get_sql_content('login', 'user_name', 'pass_word'))
